package com.lennon.eoi.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import com.lennon.eoi.model.ExcelConfigVO;

/**
 * xml 工具类
 * 
 * @author lennon
 *
 */
public class XmlUtils {
	
	public static Map<String, Object> Dom2Map(Document doc){
		Map<String, Object> map = new HashMap<String, Object>();
		if(doc == null)
			return map;
		Element root = doc.getRootElement();
		for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
			Element e = (Element) iterator.next();
			//System.out.println(e.getName());
			List list = e.elements();
			if(list.size() > 0){
				map.put(e.getName(), Dom2Map(e));
			}else
				map.put(e.getName(), e.getText());
		}
		return map;
	}
	

	public static Map Dom2Map(Element e){
		Map<String,Object>	map = new HashMap<String,Object>();
		List list = e.elements();
		if(list.size() > 0){
			for (int i = 0;i < list.size(); i++) {
				Element iter = (Element) list.get(i);
				List mapList = new ArrayList();
				
				if(iter.elements().size() > 0){
					Map m = Dom2Map(iter);
					if(map.get(iter.getName()) != null){
						Object obj = map.get(iter.getName());
						if(!obj.getClass().getName().equals("java.util.ArrayList")){
							mapList = new ArrayList();
							mapList.add(obj);
							mapList.add(m);
						}
						if(obj.getClass().getName().equals("java.util.ArrayList")){
							mapList = (List) obj;
							mapList.add(m);
						}
						map.put(iter.getName(), mapList);
					}else
						map.put(iter.getName(), m);
				}
				else{
					if(map.get(iter.getName()) != null){
						Object obj = map.get(iter.getName());
						if(!obj.getClass().getName().equals("java.util.ArrayList")){
							mapList = new ArrayList();
							mapList.add(obj);
							mapList.add(iter.getText());
						}
						if(obj.getClass().getName().equals("java.util.ArrayList")){
							mapList = (List) obj;
							mapList.add(iter.getText());
						}
						map.put(iter.getName(), mapList);
					}else
						map.put(iter.getName(), iter.getText());
				}
			}
		}else
			map.put(e.getName(), e.getText());
		return map;
	}
	
	/**
	 * 解析execl配置文件
	 * 
	 * @param doc 文档对象
	 * @return
	 */
	public static Map<String, Map<String, ExcelConfigVO>> Dom2MapWithAttrs(Document doc) {
		Map<String, Map<String, ExcelConfigVO>> map = new HashMap<String, Map<String, ExcelConfigVO>>();

		if (doc == null)
			return map;
		Element root = doc.getRootElement();
		for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
			Element e = (Element) iterator.next();

			Map<String, ExcelConfigVO> colMap = new HashMap<String, ExcelConfigVO>();

			for (Iterator colElement = e.elementIterator(); colElement.hasNext();) {
				Element element = (Element) colElement.next();
				ExcelConfigVO configVO = new ExcelConfigVO();
				BeanCopierUtils.copyPropertiesFromMap(configVO, Attribute2Map(element));

				colMap.put(element.attributeValue("key"), configVO);
			}
			map.put(e.attributeValue("id"), colMap);
		}
		return map;
	}

	public static Map<String,String> Attribute2Map(Element e){
		
		Map<String,String> attrMap = new HashMap<String,String>();
		for(Iterator it=e.attributeIterator();it.hasNext();){
			Attribute attribute = (Attribute) it.next();
			String name = attribute.getName();
			String value = attribute.getText();
			attrMap.put(name, value);
			}
		return attrMap;
	}
	 
}
